/**
 * 实验室管理数据model
 */
import {getAcPageManageList,saveAc,deleteAc,approveAc,getStuList,authAc, cancelAuthAc, getAuthPageManageList,getCountInfo} from 'api/acManageApi';
import {getLabList} from 'api/labManageApi';

const acManage = {
    state: {
        acManagePageData:[],
        authManagePageData:[],
        labSelectList:[],
        stuSelectList:[],
        page: {
            currentPage: 1,
            pageSize: 5,
            total: 0
        },
        queryPara:{
            name: '',
        }
    },
    mutations: {
        loadAcManageMut(state, obj){
            if (obj.rows !== '') {
                state.acManagePageData = obj.rows;
                state.page.currentPage = obj.currentPage;
                state.page.pageSize = obj.pageSize;
                state.page.total = obj.total;
            }
        },
        loadAuthManageMut(state, obj){
            if (obj.rows !== '') {
                state.authManagePageData = obj.rows;
                state.page.currentPage = obj.currentPage;
                state.page.pageSize = obj.pageSize;
                state.page.total = obj.total;
            }
        },
        loadLabSelectListMut(state, obj){
            let labSelectList = [];
            if (obj.code === 0) {
                if (obj.data && obj.data.length > 0){
                    obj.data.forEach((item, index) => {
                        labSelectList.push({
                            index : index,
                            value : item.recordId,
                            label : item.buildName + '-' + item.name ,
                            record:{...item}
                        })
                    })
                }
                state.labSelectList = labSelectList;
            }
        },
        loadStuSelectListMut(state, obj){
            let stuSelectList = [];
            if (obj.code === 0) {
                if (obj.data && obj.data.length > 0){
                    obj.data.forEach((item, index) => {
                        stuSelectList.push({
                            index : index,
                            value : item.id,
                            label : item.orgName + '-' + item.name ,
                            record:{...item}
                        })
                    })
                }
                state.stuSelectList = stuSelectList;
            }
        },
        setAcManageQueryPrams(state, obj){
            state.queryPara = obj;
        }

    },
    actions: {
        /*获取实验室列表*/
        getAcManagePageListAct (context, para) {
            return getAcPageManageList(para).then(res => {
                context.commit("loadAcManageMut",res.data);
            });
        },
        /*获取实验室列表*/
        getAuthPageManageListAct (context, para) {
            return getAuthPageManageList(para).then(res => {
                context.commit("loadAuthManageMut",res.data);
            });
        },
        /*新增/编辑实验室申请*/
        saveAcAct (context, para) {
            return saveAc(para).then(res=>{
                return res.data;
            });
        },
        /*删除实验室申请*/
        deleteAcAct (context, para) {
            return deleteAc(para).then(res=>{
                return res.data;
            });
        },
        /*审核实验室申请*/
        approveAcAct (context, para) {
            return approveAc(para).then(res=>{
                return res.data;
            });
        },

        /*获取实验室列表*/
        getLabListAct (context, para) {
            return getLabList(para).then(res=>{
                context.commit("loadLabSelectListMut",res.data);
            });
        },
        /*获取学生列表*/
        getStuListAct (context, para) {
            return getStuList(para).then(res=>{
                context.commit("loadStuSelectListMut",res.data);
            });
        },

        /*删除实验室*/
        authAct (context, para) {
            return authAc(para).then(res=>{
                return res.data;
            });
        },
        /*审核实验室*/
        cancelAuthAcAct (context, para) {
            return cancelAuthAc(para).then(res=>{
                return res.data;
            });
        },getCountInfoAct (context, para) {
            return getCountInfo(para).then(res=>{
                return res.data;
            });
        },

    }
};

export default acManage;
